package xcmg.device.web.sale;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.sale.SaleReverseService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.sale.SaleReverseAddVO;
import xcmg.device.service.vo.sale.SaleReverseVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.util.Arrays;

@RestController
@RequestMapping("/saleReverse")
public class SaleReverseController extends BasicController {
    @Autowired
    private SaleReverseService saleReverseService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加冲销入库记录")
    @FastMappingInfo(needLogin = true,code = AuthCode.ThreeReverse.ADD)
    public ActionResult add(@RequestBody SaleReverseAddVO saleReverseAddVO)throws Exception{
        if(null == saleReverseAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        FunctionResult result = new FunctionResult();
        ErrorCode errorCode;
        try {
            errorCode = saleReverseService.add(saleReverseAddVO, getUserId(), getOrgId());
            result.setCode(errorCode);
            result.setValue(errorCode.getDesc());
        }catch (Exception exc){
            result.setCode(ErrorCode.Failure);
            result.setValue(exc.getMessage());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @ApiOperation(value = "审核销售冲销单")
    @FastMappingInfo(needLogin = true,code =AuthCode.ThreeReverse.AUDIT)
    public ActionResult audit(@RequestBody SaleReverseVO saleReverseVO)throws Exception{
        if(null == saleReverseVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        FunctionResult result = new FunctionResult();
        ErrorCode errorCode;
        try {
            errorCode = saleReverseService.audit(saleReverseVO, getOrgId(), getUserId());
            return actionResult(errorCode);
        }catch (Exception exc){
            result.setCode(ErrorCode.Failure);
            LogHelper.monitor(getExceptionAllinformation(exc));
            return new ActionResult(ErrorCode.Failure.getCode(), exc.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("冲销入库记录删除")
    @FastMappingInfo(needLogin = true,code =AuthCode.ThreeReverse.DEL)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(saleReverseService.delete(Arrays.asList(ids), getOrgId()));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id)throws Exception{
        return actionValueResult(saleReverseService.findByPK(id, "id"));
    }

    @RequestMapping(value = "/itemByNo",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByNo(String abnormalNo)throws Exception{
        return actionValueResult(saleReverseService.findByPK(abnormalNo, "abnormalNo"));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询冲销入库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SaleReverseVO> condition)throws Exception{
        SaleReverseVO saleReverseVO =condition.getFilters();
        if(saleReverseVO == null){
            saleReverseVO = new SaleReverseVO();
        }
        saleReverseVO.setOrgId(getOrgId());
        condition.setFilters(saleReverseVO);
        PageCommonVO pageCommonVO= saleReverseService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出冲销入库记录列表")
    @FastMappingInfo(needLogin = true,code =AuthCode.ThreeReverse.EXPORT)
    public void export( String[] ids)throws Exception {
        saleReverseService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部冲销入库记录列表")
    @FastMappingInfo(needLogin = true,code =AuthCode.ThreeReverse.EXPORT)
    public void exportAll(SaleReverseVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        saleReverseService.exportAll(condition);
    }

    private String getExceptionAllinformation(Exception ex){
        String sOut = ex.getMessage()+"\r\n";
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut += "\tat " + s + "\r\n";
        }
        return sOut;
    }
}
